package com.probox.modules.controller.product;

import com.probox.common.core.enums.CategoryDepth;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.product.ProductCategory;
import com.probox.common.entity.service.product.IProductCategoryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品类别Controller
 *
 *
 * @date 2021-05-26
 */
@RestController
@RequestMapping("/product/category")
public class ProductCategoryController extends BaseController {
    @Autowired
    private IProductCategoryService productCategoryService;

    /**
     * 查询商品类别列表
     */
    @PreAuthorize(hasPermi = "product:category:list")
    @GetMapping("/list")
    public AjaxResult list(ProductCategory productCategory) {
        List<ProductCategory> list = productCategoryService.queryList(productCategory);
        return AjaxResult.success(list);
    }

    /**
     * 查询该等级的类别
     */
    @PreAuthorize(hasPermi = "product:category:list")
    @GetMapping("/depthlist/{depth}")
    public AjaxResult depthlist(@PathVariable("depth") String depth) {
        List<ProductCategory> categoryDepthList = productCategoryService.getCategoryDepthList(depth,SecurityUtils.getPlatformId());
        return AjaxResult.success(categoryDepthList);
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/selectOptions")
    public AjaxResult selectOptions()
    {
        return AjaxResult.success(productCategoryService.selectOptionList());
    }

    /**
     * 导出商品类别列表
     */
    @PreAuthorize(hasPermi = "product:category:export")
    @Log(title = "专业类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCategory productCategory) throws IOException {
        List<ProductCategory> list = productCategoryService.queryList(productCategory);
        ExcelUtil<ProductCategory> util = new ExcelUtil<ProductCategory>(ProductCategory.class);
        util.exportExcel(response, list, "专业数据");
    }

    /**
     * 获取商品类别详细信息
     */
    @PreAuthorize(hasPermi = "product:category:query")
    @GetMapping(value = "/{categoryUid}")
    public AjaxResult getInfo(@PathVariable("categoryUid") Long categoryUid) {
        return AjaxResult.success(productCategoryService.selectProductCategoryById(categoryUid));
    }

    /**
     * 新增商品类别
     */
    @PreAuthorize(hasPermi = "product:category:add")
    @Log(title = "专业类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategory productCategory) {
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    /**
     * 修改商品类别
     */
    @PreAuthorize(hasPermi = "product:category:edit")
    @Log(title = "专业类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCategory productCategory) {
        return toAjax(productCategoryService.updateProductCategory(productCategory));
    }

    /**
     * 修改商品类别状态
     */
    @PreAuthorize(hasPermi = "product:category:edit")
    @Log(title = "专业类别", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCategoryStatus")
    public AjaxResult changeStatus(@RequestBody ProductCategory productCategory) {
        return toAjax(productCategoryService.updateCategoryStatus(productCategory));
    }

    /**
     * 删除商品类别
     */
    @PreAuthorize(hasPermi = "product:category:remove")
    @Log(title = "专业类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryUids}")
    public AjaxResult remove(@PathVariable Long[] categoryUids) {
        return toAjax(productCategoryService.deleteProductCategoryByIds(categoryUids));
    }

    @ApiOperation(value = "查询专业类别顶级分类列表")
    @GetMapping("/topList")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductCategory.class),
    })
    public AjaxResult selectTopCategory(ProductCategory productCategory) {
        startPage();
        productCategory.setPlatformUid(SecurityUtils.getPlatformId());
        productCategory.setCategoryDepth(CategoryDepth.one.getKey());
        productCategory.setDataPriority(1L);
        productCategory.setDataStatus(DataStatus.valid.getKey());
        productCategory.setIsShow("1");
        List<ProductCategory> list = productCategoryService.queryList(productCategory);
        return AjaxResult.successs(list, productCategoryService.selectCategoryCount(productCategory));
    }
    //getCategoryVo
    @ApiOperation(value = "查询专业类别顶级分类下的所有子分级")
    @GetMapping("/childCategory/{categoryUid}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductCategory.class),
    })
    public AjaxResult selectChildCategory(@PathVariable("categoryUid") Long categoryUid) {
        return AjaxResult.success(productCategoryService.getCategoryVo(categoryUid));
    }

}
